package com.online.pay.web.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Date;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.online.pay.common.bean.ali.AliPayReqParams;
import com.online.pay.common.bean.wx.WxOrderInfo;
import com.online.pay.common.modle.PayTypeEnum;
import com.online.pay.common.modle.TradeResult;
import com.online.pay.common.utils.BeanMapper;
import com.online.pay.common.utils.ValidationResult;
import com.online.pay.common.utils.ValidationUtils;
import com.online.pay.common.utils.ZxingUtils;
import com.online.pay.web.bean.CreatePayBean;
import com.online.pay.web.dao.entity.OrderInfo;
import com.online.pay.web.exception.BusinessException;
import com.online.pay.web.service.AliService;
import com.online.pay.web.service.OrderInfoService;
import com.online.pay.web.service.WxService;

/**
 * 在线支付统一入口
 * @author Qiurz
 *
 */
@Controller
@RequestMapping("/pay")
public class PayController {
	
	public static final Logger logger = LoggerFactory.getLogger(PayController.class);
	
	@Autowired
	private OrderInfoService orderInfoService;
	
	@Autowired
	private AliService aliService;
	
	@Autowired
	private WxService wxService;

	@RequestMapping(value="/createOrder",method={RequestMethod.GET,RequestMethod.POST},produces = "image/jpeg;charset=UTF-8")
	public @ResponseBody byte[] createOrder(CreatePayBean createPayBean) throws Exception {
		//1.参数校验
		ValidationResult result = ValidationUtils.validateEntity(createPayBean);
		if (result.isHasErrors()) {
			throw new BusinessException(result.toString());
		}
		OrderInfo orderInfo = new OrderInfo();
		BeanMapper.copy(createPayBean,orderInfo);
		//2.订单入库
		orderInfo.setCreateDate(new Date());
		boolean saveResult = orderInfoService.save(orderInfo);
		//3.支付渠道
		if (saveResult) {
			TradeResult tradeResult = null;
			if (createPayBean.getPayWay().equals(PayTypeEnum.AliType.getType())) {
				AliPayReqParams aliPayReqParams = new AliPayReqParams();
				aliPayReqParams.setOut_trade_no(createPayBean.getOrderno());
				aliPayReqParams.setSubject(createPayBean.getThemeGoods());
				aliPayReqParams.setTotal_amount(createPayBean.getAmount());
				aliPayReqParams.setAuth_code(createPayBean.getAuthCode());
				aliPayReqParams.setScene("bar_code");
				aliPayReqParams.setTimeout_express("90m");
				tradeResult = aliService.createOrder(aliPayReqParams);
			} else if (createPayBean.getPayWay().equals(PayTypeEnum.WxType.getType())) {
				WxOrderInfo wxOrderInfo = new WxOrderInfo();
				tradeResult = wxService.createOrder(wxOrderInfo);
			}
			//4.响应结果(支付成功)
			if (tradeResult != null) {
				BufferedImage bufferedImage = ZxingUtils.writeInfoToJpgBuffImg(tradeResult.getQrCode(),
						400, 400);
				ByteArrayOutputStream stream = new ByteArrayOutputStream();
				ImageIO.write(bufferedImage, "jpeg", stream);
				return stream.toByteArray();
			}
		}
		return null;
	}
}
